package cn.shengongying.api.dto;

import cn.shengongying.bean.BaseUser;
import cn.shengongying.exception.ParamException;
import cn.shengongying.model.BossOrder;
import cn.shengongying.model.BossPosition;
import cn.shengongying.model.UserDiscountInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * @author : RXK
 * Date : 2021-7-24 18:02
 * Code Less Think More
 * Desc:
 */
@Getter
@Setter
public class OrderInDTO extends IdInDTO {


    @ApiModelProperty(value = "支付方式(0:微信支付;1:支付宝支付)")
    @NotNull(message = "支付方式不能为空")
    private Integer payType;


    @ApiModelProperty(value = "优惠券id(没用不传)")
    private Long discountId;


    @ApiModelProperty(value = "是否需要开发票(0:否;1:是)")
    @NotNull(message = "发票状态不能为空")
    private Integer invoiceStatus;

    @ApiModelProperty(value = "发票抬头")
    private String invoiceTitle;

    @ApiModelProperty(value = "税务号")
    private String texNo;

    @ApiModelProperty(value = "联系电话")
    private String mobile;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "发票内容")
    private String remark;


    public BossOrder buildOrder(BaseUser baseUser, BossPosition position, UserDiscountInfo userDiscountInfo, BigDecimal serviceCharge) {
        BossOrder order = new BossOrder();
        order.setUserId(baseUser.getId());
        order.setType(0);
        order.setPayType(this.payType);
        calculate(order, position, userDiscountInfo,serviceCharge);
        order.setInvoiceStatus(this.invoiceStatus);
        order.setExtId(position.getId());
        if (this.invoiceStatus == 1) {
            createInvoiceRecord(order);
        }
        return order;
    }

    private void createInvoiceRecord(BossOrder order) {
        if (StringUtils.isAnyEmpty(this.invoiceTitle,this.texNo,this.mobile,this.email)) {
            throw new ParamException();
        }

        order.setInvoiceTitle(this.invoiceTitle);
        order.setTaxNo(this.texNo);
        order.setMobile(this.mobile);
        order.setEmail(this.email);
        order.setRemark(this.remark);
    }

    private void calculate(BossOrder order, BossPosition position, UserDiscountInfo userDiscountInfo, BigDecimal serviceCharge) {
        order.setDiscountId(this.discountId);
        BigDecimal discount = userDiscountInfo != null ? new BigDecimal(userDiscountInfo.getMoney()) : BigDecimal.ZERO;
        order.setDiscountAmount(discount);

        BigDecimal totalAmount = BigDecimal.valueOf(position.getPeopleNum())
                .multiply(serviceCharge);
        order.setTotalAmount(totalAmount);

        BigDecimal actualAmount;
        if (discount.compareTo(totalAmount) >= 0) {
            actualAmount = BigDecimal.ZERO;
        }else {
            actualAmount = totalAmount.subtract(discount);
        }

        order.setActualAmount(actualAmount);
        order.setNumber(position.getPeopleNum());
        order.setServiceCharge(serviceCharge);
    }
}
